<?php

namespace app\admin\controller\subject;

// 引入基类控制器
use app\common\controller\Backend;

class Subject extends Backend
{
    public function __construct()
    {
        parent::__construct();

        $this->model = model('Subject.Subject');
        $this->CategoryModel = model('Subject.Category');
    }

    protected $relationSearch = true;

    protected $CategoryModel;

    public function index()
    {
        if($this->request->isAjax()){
            // 过滤请求参数中的html标签和取消两边你空白
            $this->request->filter(['strip_tags', 'trim']);
            // 获取查询参数
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->model
                ->with(['category'])
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->count();

            $list = $this->model
            ->with(['category'])
            ->where($where)
            ->order($sort, $order)
            ->limit($offset, $limit)
            ->select();

            $result = [
                'rows' => $list,
                'total' => $total
            ];

            return json($result);
        }

        return $this->view->fetch();
    }

    public function add(){
        if($this->request->isPost()){
            $params = $this->request->param('row/a');

            $data = [
                'title' => $params['title'],
                'price' => $params['price'],
                'content' => $params['content'],
                'thumbs' => $params['thumbs'],
                'cateid' => $params['cateid'],
            ];

            $result = $this->model->validate('common/Subject/Subject')->save($data);

            if($result){
                $this->success('添加课程成功');
                exit;
            }else{
                $this->error($this->model->getError());
                exit;
            }
        }

        //先将课程分类查询出来
        $catelist = $this->CategoryModel->column('id,name');
        $this->assign('catelist', build_select('row[cateid]', $catelist, [], ['class'=>'selectpicker', 'required'=>'']));

        return $this->view->fetch();
    }

    public function edit($ids = NULL){
        // 查询是否存在
        $row = $this->model->find($ids);

        if($this->request->isPost()){
            $params = $this->request->param('row/a');
            $AttachmentModel = model('common/Attachment');

            $data = [
                'id' => $ids,
                'title' => $params['title'],
                'price' => $params['price'],
                'content' => $params['content'],
                'thumbs' => $params['thumbs'],
                'cateid' => $params['cateid'],
            ];

            $result = $this->model->validate('common/Subject/Subject')->isUpdate(true)->save($data);

            if($result){
                // 是否有新的图片上传
                if($data['thumbs'] != $row['thumbs']){
                    $path = '.' . $row['thumbs'];
                    // 删除旧图片，同时删除附件信息
                    if(is_file($path) && @unlink($path)){
                        $AttachmentModel->where(['url'=>$row['thumbs']])->delete();
                    }
                }

                $this->success('修改课程成功');
                exit;
            }else{
                $this->error($this->model->getError());
                exit;
            }
        }

        if(!$row){
            $this->error(__('No Results were found'));
            exit;
        }

        $this->assign('row', $row);

        $catelist = $this->CategoryModel->column('id,name');
        $this->assign('catelist', build_select('row[cateid]', $catelist, $row['cateid'], ['class'=>'selectpicker', 'required'=>'']));

        return $this->view->fetch();
    }

    public function del($ids = NULL){
        if(!$ids){
            $this->error(__('No Results were found'));
        }

        $ids = explode(',', $ids);
        
        $list = $this->model->select($ids);

        if(!$list){
            $this->error(__('No Results were found'));
            exit;
        }

        $result = $this->model->destroy($ids);

        if($result){
            $this->success('删除成功');
            exit;
        }else{
            $this->error($this->model->getError());
            exit;
        }
    }
}
